//axios封装
import loadaxios from 'axios'
import { Toast,Indicator } from 'mint-ui'

function showToast(txt){
	Toast({
		message: txt,
		position: 'bottom',
		duration: 5000
	});
}

// axios 配置
loadaxios.defaults.timeout = 5000;

// http request 拦截器
loadaxios.interceptors.request.use(
	config => {
		Indicator.open('Loading...');
		return config;
	},
	err => {
		Indicator.close();
		showToast('请求失败');
		return Promise.reject(err);
	});

// http response 拦截器
loadaxios.interceptors.response.use(
	response=> {
		Indicator.close();
		return response;
	},
	err => {
		Indicator.close();
		if (err && err.response) {
			switch (err.response.status) {
				case 400:
					showToast('请求错误');
					break
				case 401:
					showToast('未授权，请登录');
					break
				case 403:
					showToast('拒绝访问');
					break
				case 404:
					showToast(`请求地址出错: ${err.response.config.url}`);
					break
				case 408:
					showToast('请求超时');
					break
				case 500:
					showToast('服务器内部错误');
					break
				case 501:
					showToast('服务未实现');
					break
				case 502:
					showToast('网关错误');
					break
				case 503:
					showToast('服务不可用');
					break
				case 504:
					showToast('网关超时');
					break
				case 505:
					showToast('HTTP版本不受支持');
					break
				default:
			}
		}
		return Promise.reject(err)
	});

export default loadaxios;